<template>
    <a-layout id="components-layout-demo-top-side-2">
        <a-layout-header class="header">
            <a-row type="flex" justify="space-between">
                <a-col>
                    <h1 class="logo">后台管理系统</h1>
                </a-col>
                <a-col>
                    <label>{{ user.username }}</label>
                    <a-button
                        type="primary"
                        shape="circle"
                        icon="logout"
                        @click="logout"
                    />
                </a-col>
            </a-row>
        </a-layout-header>
        <a-layout>
            <a-layout-sider width="200" style="background: #fff">
                <a-menu
                    mode="inline"
                    :default-selected-keys="['1']"
                    :default-open-keys="['sub1']"
                    :style="{ height: '100%', borderRight: 0 }"
                >
                    <a-menu-item key="student"
                        ><router-link to="/manage/student">
                            学生管理
                        </router-link></a-menu-item
                    >
                    <a-menu-item key="classes"> 班级管理 </a-menu-item>
                    <a-menu-item key="teacher"> 老师管理 </a-menu-item>
                    <a-menu-item key="product">
                        <router-link to="/manage/product">
                            商品管理
                        </router-link>
                    </a-menu-item>
                    <a-menu-item key="total">
                        <router-link to="/manage/total">
                            统计
                        </router-link>
                    </a-menu-item>
                </a-menu>
            </a-layout-sider>
            <a-layout style="padding: 0 24px 24px">
                <a-layout-content
                    :style="{
                        height: '100%',
                        background: '#fff',
                        padding: '24px',
                        margin: 0,
                        minHeight: '280px',
                    }"
                >
                    <router-view></router-view>
                </a-layout-content>
            </a-layout>
        </a-layout>
    </a-layout>
</template>

<script>
import { getUser } from "../services/user";
export default {
    data() {
        return {
            user: {},
        };
    },
    async created() {
        // try{
        //     let user = await getUser();
        // }catch(e){
        //     console.log("401");
        //     this.$router.push("/login");
        // }
    },
    async beforeRouteEnter(to, from, next) {
        // 在渲染该组件的对应路由被 confirm 前调用
        // 不！能！获取组件实例 `this`
        // 因为当守卫执行前，组件实例还没被创建
        // console.log("beforeRouteEnter");
        // try {
        //     let user = await getUser();
        //     console.log("user", user.user);
        //     next((vm) => {
        //         vm.user = user.user;
        //     });
        // } catch (e) {
        //     next("/login");
        // }
        next();
    },
    methods: {
        logout() {
            sessionStorage.removeItem("token");
            this.$router.push("/login");
        },
    },
};
</script>

<style>
#components-layout-demo-top-side-2 .logo {
    /* width: 120px;
  height: 31px;
  background: rgba(255, 255, 255, 0.2);
  margin: 16px 28px 16px 0;
  float: left; */
    color: white;
}
label {
    color: white;
}
/* html,body,#app{
    height:100%;
} */
#components-layout-demo-top-side-2 {
    height: 100%;
}
</style>